package com.css.fxfzdpzs.modules.fwss.service;

import com.css.fxfzdpzs.modules.fwss.entity.first.Fw5yearsNum;
import com.css.fxfzdpzs.modules.fwss.entity.first.*;
import com.css.fxfzdpzs.modules.fwss.entity.second.CollectionSituation;
import com.css.fxfzdpzs.modules.fwss.entity.second.FwSiftCoverage;
import com.css.fxfzdpzs.modules.fwss.entity.second.GeneralSurveyStatistics;
import com.css.fxfzdpzs.modules.fwss.entity.second.HousingStatistics;
import com.css.fxfzdpzs.modules.fwss.vo.*;
import com.css.fxfzdpzs.modules.fwss.repository.FwssRepository;
import com.css.fxfzdpzs.util.PlatformObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author y1fan
 * @description
 */
@Service
public class FwssServiceImpl implements FwssService {


    @Resource
    FwssRepository fwssRepository;


    @Override
    public FwCollectionSystemSituationVO getSystemSituation() {
        FwCollectionSystemSituationVO vo = new FwCollectionSystemSituationVO();
        List<FwCollectionSystemSituation> fwCollectionSystemSituations = fwssRepository.getSystemSituation();
        Integer today = 0;
        Integer all = 0;
        if (PlatformObjectUtils.isNotEmpty(fwCollectionSystemSituations)) {
            for (FwCollectionSystemSituation fwCollectionSystemSituation : fwCollectionSystemSituations) {
                today += fwCollectionSystemSituation.getVisitsNum();
                all += fwCollectionSystemSituation.getAllNum();
            }
        }
        vo.setVisitsTodayNumTimes(today);
        vo.setVisitsAccrueNumTimes(all);
        vo.setFwCollectionSystemSituations(fwCollectionSystemSituations);
        return vo;
    }

    @Override
    public FwProvinceSamplingRateVO getSamplingRate() {
        List<FwProvinceSamplingRate> list = fwssRepository.getNumCoverage();
        FwProvinceSamplingRateVO vo = fwssRepository.getCountrySituation();  //fw_country_situation
        vo.setDongRates(list);
        return vo;
    }

    @Override
    public FwProvinceDongAndAreaVO getProvinceSamplingRate() {
        FwProvinceDongAndAreaVO vo = new FwProvinceDongAndAreaVO();
        List<FwProvinceDongAndArea> siftAll = fwssRepository.getSubmitSituation("0", "0");//详查全部
        List<FwProvinceDongAndArea> siftCheck = fwssRepository.getSubmitSituation("0", "1");//详查审核
        List<FwProvinceDongAndArea> generalAll = fwssRepository.getSubmitSituation("1", "0");//普查全部
        List<FwProvinceDongAndArea> generalCheck = fwssRepository.getSubmitSituation("1", "1");//普查审核
        vo.setGeneralAll(generalAll);
        vo.setGeneralCheck(generalCheck);
        vo.setSiftAll(siftAll);
        vo.setSiftCheck(siftCheck);
        return vo;
    }

    @Override
    public GeneralSituationVO getGeneralSituation() {
        GeneralSituationVO vo = new GeneralSituationVO();
        //  fw_general_library_situation
        List<GeneralSituation> all = fwssRepository.getGeneralLibrarySituation("0");
        List<GeneralSituation> check = fwssRepository.getGeneralLibrarySituation("1");
        vo.setAll(all);
        vo.setCheck(check);
        return vo;
    }

    @Override
    public SiftSituationVO getSiftSituation() {
        SiftSituationVO vo = new SiftSituationVO();
        //已入库数据 fw_sift_library_situation
        List<SiftSituation> all = fwssRepository.getSiftLibrarySituation("0");
        List<SiftSituation> check = fwssRepository.getSiftLibrarySituation("1");
        vo.setAll(all);
        vo.setCheck(check);
        return vo;
    }

    @Override
    public List<Fw5yearsNum> getYearsNum() {
        return fwssRepository.getYearsNum();
    }

    @Override
    public List<HousingStatistics> getStatistics() {
        return fwssRepository.getStatistics();
    }

    @Override
    public List<CollectionSituation> getSituation() {
        return fwssRepository.getSituation();
    }

    @Override
    public Map<String, List<GeneralSurveyStatistics>> getGeneralSurvey(String mark) {
        String classify = null;
        List<String> list = new ArrayList<>();
        if ("0".equals(mark)) {
            classify = "0";
            //乡村面积占比 classify为 0 ,category为 0
            list.add("0");
            //城镇面积占比 classify为 0 ,category为 1
            list.add("1");
        }
        if ("1".equals(mark)) {
            classify = "1";
            list.add("0");
            list.add("1");
        }
        if ("2".equals(mark)) {
            classify = "0";
            list.add("2");
            list.add("3");
            list.add("4");
        }
        if ("3".equals(mark)) {
            classify = "1";
            list.add("2");
            list.add("3");
            list.add("4");
        }
        List<GeneralSurveyStatistics> list1 = fwssRepository.getData1(classify, list);
        Map<String, List<GeneralSurveyStatistics>> map = list1.stream().collect(Collectors.groupingBy(GeneralSurveyStatistics::getCategory));
        for (Map.Entry<String, List<GeneralSurveyStatistics>> entry : map.entrySet()) {
            List<GeneralSurveyStatistics> collect = entry.getValue().stream().sorted(Comparator.comparing(s -> Integer.valueOf(s.getType()))).collect(Collectors.toList());
            entry.setValue(collect);
        }
        return map;
    }

    @Override
    public List<FwMapDate> getMapDate(String flag) {
        return fwssRepository.getMapDate(flag);
    }

    @Override
    public FwSiftCoverageVO getSiftCoverage() {
        FwSiftCoverageVO vo=new FwSiftCoverageVO();
      List<FwSiftCoverage> areaAll=  fwssRepository.getSiftCoverage("0");
      List<FwSiftCoverage> areaCheck=  fwssRepository.getSiftCoverage("1");
      List<FwSiftCoverage> dongAll=  fwssRepository.getSiftCoverage("2");
      List<FwSiftCoverage> dongCheck=  fwssRepository.getSiftCoverage("3");
      vo.setAreaAll(areaAll);
      vo.setAreaCheck(areaCheck);
      vo.setDongAll(dongAll);
      vo.setDongCheck(dongCheck);
        return vo;
    }


}
